package com.biyao.service.model.base.imp;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.HashMap;
import java.util.Map;

import com.biyao.schedulingdao.base.TBearingMapper;
import com.biyao.schedulingentity.base.TBearingEntity;
import com.biyao.schedulingentity.base.TBearingEntityCopy;
import com.biyao.schedulingentity.base.TRollerBasicEntity;
import com.biyao.service.model.base.TRollerBasicMessage;
import com.biyao.service.model.state.RollerExtMessage;
import com.biyao.service.model.state.imp.TRollerExtService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;
/**
 * 
 *
 * @author code generator
 * @date 2020-04-26 17:59:59
 */
@Service
public class TBearingService extends ServiceImpl<TBearingMapper, TBearingEntity>{

    @Autowired
    private TRollerBasicMessage tRollerBasicMessage;
    @Autowired
    private RollerExtMessage rollerExtMessage;
    public Page list(int pageNum, int pageSize){

        EntityWrapper<TBearingEntity> tBearingentityWrapper=new EntityWrapper<>();
        Page page=selectPage(new Page(pageNum,pageSize),tBearingentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TBearingEntity> list){
        List resList=new ArrayList();
        for(TBearingEntity tBearingEntity:list){
            Map map=formatEntity(tBearingEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TBearingEntity tBearingEntity){
        Map map=new HashMap(16);
        //
        if(tBearingEntity.getId()!=null){
           map.put("id",tBearingEntity.getId());
        }
        //
        if(tBearingEntity.getCode()!=null){
           map.put("code",tBearingEntity.getCode());
        }
        //
        if(tBearingEntity.getManufacturer()!=null){
           map.put("manufactor",tBearingEntity.getManufacturer());
        }
        //
        if(tBearingEntity.getTime()!=null){
           map.put("time",tBearingEntity.getTime());
        }
        //
        if(tBearingEntity.getCondition()!=null){
           map.put("condition",tBearingEntity.getCondition());
        }
        //
        if(tBearingEntity.getScrap()!=null){
           map.put("scrap",tBearingEntity.getScrap());
        }
        //
        if(tBearingEntity.getInitState()!=null){
           map.put("initState",tBearingEntity.getInitState());
        }
        //
        if(tBearingEntity.getNote()!=null){
           map.put("note",tBearingEntity.getNote());
        }
        //
        if(tBearingEntity.getRollerNewId()!=null){
           map.put("rollerNewId",tBearingEntity.getRollerNewId());
           TRollerBasicEntity tRollerBasicEntity= tRollerBasicMessage.selectRollerBasicById(tBearingEntity.getRollerNewId());
           map.put("tRollerBasicCode",tRollerBasicEntity.getCode());
        }
        //
        if(tBearingEntity.getDiameter()!=null){
           map.put("diameter",tBearingEntity.getDiameter());
        }
        //
        if(tBearingEntity.getRollerBackId()!=null){
           map.put("rollerBackId",tBearingEntity.getRollerBackId());
        }
        //
        if(tBearingEntity.getBoxState()!=null){
           map.put("boxState",tBearingEntity.getBoxState());
        }
        //
        if(tBearingEntity.getKeepState()!=null){
           map.put("keepState",tBearingEntity.getKeepState());
        }
        //
        if(tBearingEntity.getKeepTime()!=null){
           map.put("keepTime",tBearingEntity.getKeepTime());
        }
        return map;
    }


    public TBearingEntityCopy selectNowState(Integer id){
        TBearingEntity tBearingEntity= selectById(id);
        TBearingEntityCopy tBearingEntityCopy=new TBearingEntityCopy();
        BeanUtils.copyProperties(tBearingEntity,tBearingEntityCopy);
        Integer rollerId=tBearingEntity.getRollerNewId();
        if(rollerId!=null){
            Map map=rollerExtMessage.selectRollerExtByRollerId(rollerId);
            if(tBearingEntity.getRollerBackId()!=null){
                tBearingEntityCopy.setRollerBackId(tBearingEntity.getRollerBackId());
            }else {
                tBearingEntityCopy.setRollerBackId((Integer)map.get("rollerRackId"));
            }
            tBearingEntityCopy.setOperateBox((String)map.get("operateBox"));
            tBearingEntityCopy.setTranBox((String)map.get("tranBox"));
            tBearingEntityCopy.setParRollerCode((String)map.get("parRollerCode"));
        }
        return tBearingEntityCopy;
    }

    public void updateTBearingCopy(TBearingEntityCopy tBearing){
        TBearingEntity tBearingEntity=new TBearingEntity();
        BeanUtils.copyProperties(tBearing,tBearingEntity);
        updateById(tBearingEntity);
    }

    public List<Map> selectBearingReport(){
        EntityWrapper<TBearingEntity> tBearingentityWrapper=new EntityWrapper<>();
        List<Map> list=formatRecords(selectList(tBearingentityWrapper));
        if(list!=null&&list.size()>0){
            for(Map map:list){
                Integer rollerId=(Integer)map.get("rollerNewId");
                if(rollerId!=null){
                    Map map1=rollerExtMessage.selectRollerExtByRollerId(rollerId);
                    map.put("operateBox",(String)map1.get("operateBox"));
                    map.put("parRollerCode",(String)map1.get("parRollerCode"));
                }

            }
        }
        return list;
    }

}

